package org.coldlife.sim.grav;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import org.coldlife.sim.display.MainWindow;


public class GravSim 
{
	private World world;
	private MainWindow win;
	
	private Thread simThread;
	private SimulationTimer simTimer;
	
	private int num_particles = 0;
	private int sizeX = 0;
	private int sizeY = 0;
	
	public void initDisplay() 
	{
		win = new MainWindow(getWorld());
		win.setSize(sizeX, sizeY);
		
		win.addWindowListener(new WindowAdapter()
		{
			@Override
			public void windowClosed(WindowEvent e) 
			{
				simTimer.setDone(true);
			}
		});
	}
	
	public void startSim()
	{
		setWorld(new World(num_particles));
		simTimer = new SimulationTimer(getWorld());
		simThread = new Thread(simTimer, "GravSim.simThread");
		simThread.start();
	}

	public void startDisplay() 
	{
		if(win==null)
			throw new IllegalStateException("Display has not been initialized");
		
		win.start();
	}

	// getter setter
	public int getNum_particles() {return num_particles;}
	public int getSizeX() {return sizeX;}
	public int getSizeY() {return sizeY;}
	public void setNum_particles(int num_particles) {this.num_particles = num_particles;}
	public void setSizeX(int sizeX) {this.sizeX = sizeX;}
	public void setSizeY(int sizeY) {this.sizeY = sizeY;}
	
	public World getWorld(){ return this.world;}
	private void setWorld(World world)
	{
		this.world = world;
		if(win!=null){win.setWorld(getWorld());}
	}
}
